#ifndef PUREFTPD_MONITOR_H
#define PUREFTPD_MONITOR_H

#include "../ftpmonitorloop.h"

/*
 *
 *
*/
class PureftpdMonitor : public FTPMonitorLoop
{
	public:

		/*
		 *
		 */
		PureftpdMonitor(QString whoPath,bool useSudo)
		:FTPMonitorLoop()
		{
			createExecCommand(useSudo,whoPath);
		}

		// <client pid="17474" account="ftp" time="27" state=" DL " 
		// file="/uploads/archlinux-2009.02-core-i686.iso" host="sarka.xxxx.xx" localhost="minime.xxxx.cz" localport="21" />
		//
		// <client pid="17435" account="ftp" time="92" state="IDLE" file="" host="sarka.xxxx.xx" localhost="minime.xxxx.xx" localport="21" />
		void processStdOut(QString stdout)
		{
			QVector<QStringList> listOfClients;
			QStringList clientRow;
			QStringList split;
			
			QStringList raw_string_list = QString(stdout).split("\n");
			QStringList::Iterator it = raw_string_list.begin();
			
			for ( ; it != raw_string_list.end() ; it++ ) {
					split = it->split("|");
					//one line should countain 12 coulumns
					if (split.count()!=12) continue;
					//"PID" << "Login" << "State" << "file" < "Host");
					//pid|acct|time|state|file|peer|local|port|current|total|%|bandwidth
					clientRow.clear();
					clientRow << split.at(0) << split.at(1) << split.at(3) /*+ " " + split.at(10) + "%"*/ << split.at(4) << split.at(5);
					listOfClients.push_back(clientRow);
			}
			
			searchForClientsStateChange(listOfClients);
		}
		
		void createExecCommand(bool useSudo,QString whoPath)
		{
			if (whoPath=="")
				mFtpWhoCommand.bin  = "/usr/sbin/" + ftpWhoBinaryName();
			else
				mFtpWhoCommand.bin = whoPath + ftpWhoBinaryName();

			mFtpWhoCommand.args << "-s";

			if (useSudo==true)
			{
				mFtpWhoCommand.args.prepend(mFtpWhoCommand.bin);
				//The -n (non-interactive) option prevents sudo from prompting the user
				//for a password.  If a password is required for the command to run, sudo
				//will display an error messages and exit.
				mFtpWhoCommand.args.prepend("-n");
				mFtpWhoCommand.bin="sudo";
			}
		}
		
		/*
		 *
		 */
		bool ftpClientDiff(QStringList newClientRow,QStringList oldClientRow)
		{
			//check also login, it's undefined (?) until user enter name
			if (newClientRow.at(1) != oldClientRow.at(1) || newClientRow.at(2) != oldClientRow.at(2) ||
			    newClientRow.at(3) != oldClientRow.at(3) )
				return true;
			else
				return false;
		}
		
		/*
		 *
		 */
		QStringList headerLabels()
		{
			return QStringList() << "PID" << "Login" << "State" << "File" << "Host";
		}

		/*
		 *
		 */
		FtpServerType ftpServerType()
		{
			return FTPMonitorLoop::pureftpd;
		}

		/*
		 *
		 */
		QString ftpdServerName()
		{
			return QString("pureftpd");
		}

		QString static ftpWhoBinaryName()
		{
			return QString("pure-ftpwho");
		}

};

#endif
